package com.example.stray_animal_management_system.admin.controller;

import com.example.stray_animal_management_system.admin.service.SystemSettingService;
import com.example.stray_animal_management_system.entity.SystemSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: panpan
 * @time: 2025/11/9
 */
@RestController
@RequestMapping("/api/admin/system-setting")
public class SystemSettingController {

    @Autowired
    private SystemSettingService systemSettingService;

    /**
     * 获取所有设置列表
     */
    @GetMapping("/list")
    public Map<String, Object> list() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<SystemSetting> settings = systemSettingService.getAllSettings();
            result.put("code", 200);
            result.put("msg", "获取成功");
            result.put("data", settings);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取失败：" + e.getMessage());
            result.put("data", null);
        }
        return result;
    }

    /**
     * 根据ID获取设置
     */
    @GetMapping("/{id}")
    public Map<String, Object> getById(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            SystemSetting setting = systemSettingService.getById(id);
            if (setting != null) {
                result.put("code", 200);
                result.put("msg", "获取成功");
                result.put("data", setting);
            } else {
                result.put("code", 404);
                result.put("msg", "设置不存在");
                result.put("data", null);
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取失败：" + e.getMessage());
            result.put("data", null);
        }
        return result;
    }

    /**
     * 根据键名获取设置
     */
    @GetMapping("/key/{key}")
    public Map<String, Object> getByKey(@PathVariable String key) {
        Map<String, Object> result = new HashMap<>();
        try {
            SystemSetting setting = systemSettingService.getByKey(key);
            if (setting != null) {
                result.put("code", 200);
                result.put("msg", "获取成功");
                result.put("data", setting);
            } else {
                result.put("code", 404);
                result.put("msg", "设置不存在");
                result.put("data", null);
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取失败：" + e.getMessage());
            result.put("data", null);
        }
        return result;
    }

    /**
     * 添加设置
     */
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody SystemSetting setting) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (setting.getSettingKey() == null || setting.getSettingKey().trim().isEmpty()) {
                result.put("code", 400);
                result.put("msg", "键名不能为空");
                return result;
            }
            if (setting.getSettingValue() == null || setting.getSettingValue().trim().isEmpty()) {
                result.put("code", 400);
                result.put("msg", "值不能为空");
                return result;
            }
            if (setting.getType() == null || setting.getType().trim().isEmpty()) {
                setting.setType("parameter");
            }
            if (setting.getStatus() == null) {
                setting.setStatus(1);
            }
            boolean success = systemSettingService.addSetting(setting);
            if (success) {
                result.put("code", 200);
                result.put("msg", "添加成功");
            } else {
                result.put("code", 500);
                result.put("msg", "添加失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "添加失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新设置
     */
    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody SystemSetting setting) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (setting.getId() == null) {
                result.put("code", 400);
                result.put("msg", "ID不能为空");
                return result;
            }
            boolean success = systemSettingService.updateSetting(setting);
            if (success) {
                result.put("code", 200);
                result.put("msg", "更新成功");
            } else {
                result.put("code", 500);
                result.put("msg", "更新失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "更新失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除设置
     */
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = systemSettingService.deleteSetting(id);
            if (success) {
                result.put("code", 200);
                result.put("msg", "删除成功");
            } else {
                result.put("code", 500);
                result.put("msg", "删除失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "删除失败：" + e.getMessage());
        }
        return result;
    }
}

